<template>
  <!-- 修改资料 -->

  <div>
    <el-form :model="form" label-position="right" ref="formRef">
      <el-row :gutter="15">
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="编号">
            <el-input v-model="form.number" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="名称" required>
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="业务类型" required>
            <el-select
              v-model="form.promotionType"
              clearable
              placeholder="请选择："
            >
              <el-option
                v-for="item in configData.promotionTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="用户群" required>
            <el-select
              v-model="form.userGroupType"
              clearable
              placeholder="请选择："
            >
              <el-option
                v-for="item in configData.userGroupTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="会员级别">
            <el-select
              v-model="form.memberLevelIdsArray"
              multiple
              clearable
              placeholder="请选择："
            >
              <el-option
                v-for="item in configData.memberLevelList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="生效时间">
            <el-date-picker
              v-model="form.startDateTime"
              type="datetime"
              format="YYYY/MM/DD hh:mm:ss"
              value-format="YYYY-MM-DD hh:mm:ss"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="截止时间">
            <el-date-picker
              v-model="form.endDateTime"
              type="datetime"
              format="YYYY/MM/DD hh:mm:ss"
              value-format="YYYY-MM-DD hh:mm:ss"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="能否使用优惠券" required>
            <el-switch v-model="form.canUseCoupon"></el-switch>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="能否使用积分" required>
            <el-switch v-model="form.canUsePoints"></el-switch>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="能否包邮" required>
            <el-switch v-model="form.freeShipping"></el-switch>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="是否选定商品范围" required>
            <el-switch v-model="form.selectGoodsRange"></el-switch>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="状态">
            <el-select v-model="form.status" clearable placeholder="请选择：">
              <el-option
                v-for="item in configData.statusList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <template  v-if="form.selectGoodsRange===true" >
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="适用产品">
            <el-select
              v-model="form.useGoodRange"
              placeholder="请选择："
            >
              <el-option
                v-for="item in configData.useGoodRangeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="form.useGoodRange == '1'" :span="24">
          <el-form-item label="产品类别">
            <el-tree-select
              style="width: 100%"
              v-model="form.goodsCategoryIdsArray"
              :data="parentData"
              :render-after-expand="false"
              check-strictly="true"
              multiple
              show-checkbox
            />
          </el-form-item>
        </el-col>
        <template v-if="form.useGoodRange == '2' || form.useGoodRange == '3'">
          <el-form-item label="选择商品" required>
            <sc-table-select
              v-model="productDataList"
              fileName="ProductReport"
              :props="productProps"
              multiple
              :inputWidth="500"
              :mapping="productMapping"
              :maxCollapseTags="4"
            ></sc-table-select>
          </el-form-item>
          <el-table :data="productDataList">
            <el-table-column prop="number" label="编号"></el-table-column>
            <el-table-column prop="name" label="名称"></el-table-column>
            <el-table-column prop="mtlKindName" label="类别"></el-table-column>
            <el-table-column prop="spec" label="规格"></el-table-column>
            <el-table-column prop="price" label="价格"></el-table-column>
          </el-table>
        </template>
        </template>
        <el-col :span="24">
          <el-form-item label="优惠规则">
            <sc-table-select
              v-model="dataList"
              fileName="ChooseCouponReport"
              :props="CouponReportProps"
              multiple
              injectionKey="promotion"
              :inputWidth="500"
              :maxCollapseTags="4"
            ></sc-table-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-table :data="dataList">
            <el-table-column prop="number" label="编号"></el-table-column>
            <el-table-column prop="name" label="名称"></el-table-column>
            <el-table-column
              prop="transactionKindName"
              label="业务类型"
            ></el-table-column>
            <el-table-column
              prop="ruleTypeName"
              label="规则类型"
            ></el-table-column>
            <el-table-column
              prop="couponAmount"
              label="优惠价格"
            ></el-table-column>
            <el-table-column
              prop="orderMinAmount"
              label="满价格"
            ></el-table-column>
            <el-table-column
              prop="useGoodRangeName"
              label="适用产品"
            ></el-table-column>
          </el-table>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注">
            <tsEditor v-model="form.remark" height="300"></tsEditor>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- </template> -->
    <!-- <template #footer> -->
    <div class="button-position">
      <el-button @click="updateVisible">取 消</el-button>
      <el-button type="primary" @click="updatePromotion(formRef)"
        >确 定</el-button
      >
    </div>
    <!-- </template> -->

    <!-- </myDialog> -->
  </div>
</template>
    
    
  <script setup>
import { reactive, ref, onMounted } from "vue";
import { ElNotification } from "element-plus";
import tsEditor from "@/components/scEditor/index.vue";
import Api from "@/api/model/mall";
const emit = defineEmits(["updatetable"]);
const props = defineProps({
  paramsData: {
    type: Object,
    defalut: {},
  },
});
// 添加用户数据的对象
const form = ref({});
const dataList = ref([]);
const productDataList = ref([]);
const configData = ref({});
const parentData = ref([]);
const CouponReportProps = {
  value: "id",
  label: "name",
};
const productProps = {
  value: "mtlId",
  label: "name",
};
const productMapping = [
  { value: "id", label: "mtlId" },
  { value: "number", label: "number" },
  { value: "name", label: "name" },
  { value: "mtlKindName", label: "mtlKindName" },
  { value: "spec", label: "spec" },
  { value: "price", label: "price" },
];
const formRef = ref(null);
onMounted(() => {
  getInit();
});

const updateVisible = () => {
  emit("updatetable");
};

const getInit = async () => {
  let res = await Api.promotion.base.detail.post(props.paramsData);
  console.log(res);
  if (res.code == "200") {
    form.value = res.data;
    configData.value = res.configData;
    dataList.value = res.couponList || [];
    productDataList.value = res.goodsList;
  }
  let treeRes = await Api.coupon.tree.post({
    fileName: "ProductCategoryTreeReport",
  });
  if (treeRes.code == "200") {
    parentData.value = treeRes.reportData;
  }
};
const restAddFormData = () => {
  formRef.value.resetFields();
};

// 上传
const updatePromotion = async (formRef) => {
  await formRef.validate((valid, fields) => {
    if (valid) {
      updatePromotionData();
    }
  });
};

const updatePromotionData = async () => {
  form.value.couponIds = dataList.value
    .map((ele) => {
      return ele.id;
    })
    .toString();
  form.value.goodsIds = productDataList.value
    .map((ele) => {
      return ele.mtlId;
    })
    .toString();
  let params = {
    bean: form.value,
  };
  console.log(params);
  let res = await Api.promotion.base.update.post(params);
  if (res.code == "200") {
    // restAddFormData();
    ElNotification.success({
      title: "",
      message: "修改成功！",
      duration: 2000,
    });
    emit("updatetable", true);
  }
};
</script>
    
    <style scoped></style>
    
    